<template>
  <!-- 添加或修改商品对话框 -->
  <el-dialog :close-on-click-modal="false" :title="formType" :visible.sync="open" @close="cancel"
             width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">

      <el-row>
        <el-col :span="12">
          <el-form-item label="商品类目" prop="categoryId">
            <el-cascader :options="treeGoodsCategory"
                         v-model="selectCategoryData"
                         :show-all-levels="false"
                         :props="{ expandTrigger: 'hover',label:'name',value:'id' }"></el-cascader>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="封面图" v-if="form.pid != 0">
            <el-image class="res-image" v-if="form.image != null && form.image != ''" :src="form.image"
                      :preview-src-list="[form.image]"/>
            <upload-oss-btn ref="upload" :btnText="form.image == null || form.image == ''? '快速上传':'重新上传'"
                            remark="宽:350  高:350"
                            @onUploadResult="onUploadResult"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="商品名字" prop="goodsName">
            <el-input v-model="form.goodsName" placeholder="请输入商品名字"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictValue">{{dict.dictLabel}}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="是否置顶">
            <el-radio-group v-model="form.isTop">
              <el-radio
                v-for="dict in appYesNoOptions"
                :key="dict.dictValue"
                :label="dict.dictValue">{{dict.dictLabel}}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="首页推荐">
            <el-radio-group v-model="form.isRecomm">
              <el-radio
                v-for="dict in appYesNoOptions"
                :key="dict.dictValue"
                :label="dict.dictValue">{{dict.dictLabel}}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="排序(倒序)">
            <el-input-number v-model="form.sort" :min="1" placeholder="请输入排序"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="SKU分组" prop="sku">
            <el-tag :key="item.name" v-for="(item,index) in form.skuGroup" :closable="formType == $formType.add" :disable-transitions="false"
                    @close="handleClose(item)"> {{item.name}}
            </el-tag>
            <el-input class="input-new-tag" v-if="inputVisible"
                      v-model="inputValue"
                      ref="saveTagInput" size="small" @keyup.enter.native="handleInputConfirm()"
                      @blur="handleInputConfirm()">
            </el-input>
            <el-button v-else class="button-new-tag" size="small" @click="showInput()" v-if="formType == $formType.add">添加分组</el-button>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-form-item label="商品图片">
            <upload-photo-view :multiple="true" @onUploadResult="onUploadImageResult"
                               remark="宽:750  高:700"
                               ref="uploadImage"/>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-form-item label="详情图列">
            <upload-photo-view :multiple="true" :uploadStyle="3" ref="uploadDetails"
                               :upViewWidth="400" :upViewHeight="50"
                               remark="建议尺寸-宽:750 高:任意"
                               @onUploadResult="onUploadDetailsResult"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import operationModule from '@/mixins/operation-module'
  import {
    addGoods,
    updateGoods
  } from '@/api/goods/goods'
  import {
    treeGoods_category
  } from '@/api/goods/goods_category'
  import UploadOssBtn from '@/components/UploadOssBtn'
  import UploadPhotoView from '@/components/UploadPhotoView'

  export default {
    name: 'add-or-update',
    components: { UploadPhotoView, UploadOssBtn },
    mixins: [operationModule],
    watch: {
      selectCategoryData: function(value) {
        if (value.length == 0) {
        } else {
          this.form.categoryId = value[1]
        }
      },
      open(val) {
        if (val == true && this.formType == this.$formType.edit) {
          this.$nextTick(() => {
            for (let i = 0; i < this.form.imageOss.length; i++) {
              let item = this.form.imageOss[i]
              item.image = item.resources
            }
            this.$refs.uploadImage.setList(this.form.imageOss)

            for (let i = 0; i < this.form.detailsOss.length; i++) {
              let item = this.form.detailsOss[i]
              item.image = item.resources
            }
            this.$refs.uploadDetails.setList(this.form.detailsOss)
          })
        }
      }
    },
    data() {
      return {
        // 设置属性
        mixinOperationModuleOptions: {
          saveURL: addGoods, // 提交数据接口，api地址
          updateURL: updateGoods // 提交数据接口，api地址
        },
        moduleName: 'Goods',
        open: false,
        // 弹出层标题
        //表单操作类型
        formType: this.$formType.add,
        // 表单参数
        form: {},
        selectCategoryData: null,
        // 状态;0=已上架,1=未上架字典
        statusOptions: [],
        appYesNoOptions: [],
        // 表单校验
        rules: {},
        treeGoodsCategory: [],
        inputVisible: false,
        inputValue: ''
      }
    },
    methods: {
      init(type) {
        this.reset()
        this.open = true
        if (type == this.$formType.add) {
        } else if (type == this.$formType.edit) {
        } else if (type == this.$formType.detail) {
        }
        this.formType = type
      },
      initTreeGoodsCategory() {
        treeGoods_category().then(response => {
          this.treeGoodsCategory = response.data
          for (let i = 0; i < response.data.length; i++) {
            let item = response.data[i]
            let children = item.children
            if (children != null) {
              for (let j = 0; j < children.length; j++) {
                let c = children[j]
                if (this.form.categoryId == c.id) {
                  this.selectCategoryData = [item.id, c.id]
                  break
                }
              }
            }
          }
        })
      },
      onUploadResult(image) {
        this.form.image = image
      },
      onStartSbmitForm() {
        this.form.imageOss = this.$refs.uploadImage.getList()
        this.form.detailsOss = this.$refs.uploadDetails.getList()
        return true
      },
      onUploadImageResult(image) {
        let data = { id: null, image: image, sort: 0 }
        this.$refs.uploadImage.addData(data)
      },
      onUploadDetailsResult(image) {
        let data = { id: null, image: image, sort: 0 }
        this.$refs.uploadDetails.addData(data)
      },
      showInput() {
        let _this = this
        this.inputVisible = true
        this.$nextTick(_ => {
          _this.$refs.saveTagInput.$refs.input.focus()
        })
      },
      handleClose(item) {
        this.form.skuGroup.splice(this.form.skuGroup.indexOf(item), 1)
      },
      handleInputConfirm() {
        let inputValue = this.inputValue
        if (inputValue != null && inputValue != '') {
          this.form.skuGroup.push({
            name: inputValue
          })
        }
        this.inputVisible = false
        this.inputValue = ''
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          goodsName: undefined,
          status: '1',
          sort: 0,
          isTop: '1',
          isRecomm: '1',
          image: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          remark: undefined,
          categoryId: undefined,
          skuGroup: [],
          imageOss: [],
          detailsOss: []
        }
        this.selectCategoryData = []
        this.resetForm('form')
      },
      cancel() {
        this.$emit('on-close')
        this.open = false
        this.form.skuGroup = []
        this.form.imageOss = []
        this.form.detailsOss = []
        this.$refs.uploadImage.setList(null)
        this.$refs.uploadDetails.setList(null)
      }
    },
    created() {
      this.reset()
    }
  }
</script>

<style lang="scss">
  .el-tag + .el-tag {
    margin-left: 10px;
  }

  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }

  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }

  .res-image {
    width: 150px;
    height: 150px;
    border-radius: 6px;
  }
</style>
